from django.db import models
# from django.contrib.auth.models import User

# django-mptt
from mptt.models import MPTTModel, TreeForeignKey
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

# Create your models here.

# 心情的评论
class Comment(MPTTModel):

    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

    user = models.ForeignKey(
        to='account.UserInfo',
        on_delete=models.CASCADE,
        related_name='mood_comment_user',
    )
    body = models.TextField(verbose_name='评论内容', max_length=150)

    # 新增，mptt树形结构
    parent = TreeForeignKey(
        'self',
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='children'
    )

    # 新增，记录二级评论回复给谁, str
    reply_to = models.ForeignKey(
        to='account.UserInfo',
        null=True,
        blank=True,
        on_delete=models.CASCADE,
        related_name='replyers'
    )

    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    # 替换 Meta 为 MPTTMeta
    # class Meta:
    #     ordering = ('created',)
    class MPTTMeta:
        order_insertion_by = ['-create_datetime',]

    def __str__(self):
        return self.body[:20]



